package cn.youqu.happy.module.youqu.controller.app.arrangeEdit;

import cn.youqu.happy.framework.common.pojo.CommonResult;
import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.security.core.annotations.PreAuthenticated;
import cn.youqu.happy.module.youqu.controller.admin.arrangeEdit.vo.ArrangeEditPageReqVO;
import cn.youqu.happy.module.youqu.controller.admin.arrangeEdit.vo.ArrangeEditUpdateReqVO;
import cn.youqu.happy.module.youqu.controller.app.arrangeEdit.vo.*;
import cn.youqu.happy.module.youqu.convert.arrangeEdit.ArrangeEditConvert;
import cn.youqu.happy.module.youqu.dal.dataobject.arrangeEdit.ArrangeEditDO;
import cn.youqu.happy.module.youqu.service.arrangeEdit.ArrangeEditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.youqu.happy.framework.common.pojo.CommonResult.success;
import static cn.youqu.happy.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "用户 APP - 计划申请修改")
@RestController
@RequestMapping("/youqu/arrange-edit")
@Validated
public class AppArrangeEditController {

    @Resource
    private ArrangeEditService arrangeEditService;

    @GetMapping("/can-create")
    @ApiOperation("是否可以创建编辑申请")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<Boolean> canCreateArrangeEdit(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();
        return success(arrangeEditService.canCreateArrangeEdit(id, loginUserId));
    }

    @PostMapping("/create")
    @ApiOperation("创建计划申请修改")
    @PreAuthenticated
    public CommonResult<Integer> createArrangeEdit(@Valid @RequestBody AppArrangeEditCreateReqVO createReqVO) {
        Long loginUserId = getLoginUserId();
        return success(arrangeEditService.createArrangeEdit(createReqVO, loginUserId));
    }

    @PostMapping("/state")
    @ApiOperation("计划申请表态")
    @PreAuthenticated
    public CommonResult<Boolean> stateArrangeEdit(@Valid @RequestBody AppArrangeEditStateReqVO stateReqVO) {
        Long loginUserId = getLoginUserId();
        return success(arrangeEditService.stateArrangeEdit(stateReqVO, loginUserId));
    }

    @PutMapping("/update")
    @ApiOperation("更新计划申请修改")
    @PreAuthenticated
    public CommonResult<Boolean> updateArrangeEdit(@Valid @RequestBody AppArrangeEditUpdateReqVO updateReqVO) {
        ArrangeEditUpdateReqVO convert = ArrangeEditConvert.INSTANCE.convert(updateReqVO);
        Long loginUserId = getLoginUserId();
        arrangeEditService.updateArrangeEdit(convert, loginUserId);
        return success(true);
    }

    @PostMapping("/cancel")
    @ApiOperation("取消计划申请修改")
    @PreAuthenticated
    public CommonResult<Boolean> cancelArrangeEdit(@Valid @RequestBody AppArrangeEditCancelReqVO cancelReqVO) {
        Long loginUserId = getLoginUserId();
        return success(arrangeEditService.cancelArrangeEdit(cancelReqVO.getId(), cancelReqVO.getReason(), loginUserId));
    }

    @GetMapping("/get")
    @ApiOperation("获得计划申请修改")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<AppArrangeEditRespVO> getArrangeEdit(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();
        ArrangeEditDO arrangeEdit = arrangeEditService.getArrangeEdit(id, loginUserId);
        return success(ArrangeEditConvert.INSTANCE.convert0(arrangeEdit));
    }

    @GetMapping("/list")
    @ApiOperation("获得计划申请修改列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthenticated
    public CommonResult<List<AppArrangeEditRespVO>> getArrangeEditList(@RequestParam("ids") Collection<Integer> ids) {
        Long loginUserId = getLoginUserId();
        List<ArrangeEditDO> list = arrangeEditService.getArrangeEditList(ids, loginUserId);
        return success(ArrangeEditConvert.INSTANCE.convertList0(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得计划申请修改分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppArrangeEditRespVO>> getArrangeEditPage(@Valid AppArrangeEditPageReqVO pageVO) {
        Long loginUserId = getLoginUserId();
        ArrangeEditPageReqVO convert = ArrangeEditConvert.INSTANCE.convert(pageVO);
        PageResult<ArrangeEditDO> pageResult = arrangeEditService.getArrangeEditPage(convert, loginUserId);
        return success(ArrangeEditConvert.INSTANCE.convertPage0(pageResult));
    }
}
